Benchmarking spatial mapping of cell types in the mouse brain

Loading packages

1. Loading cell2location results

2. Extract gold standard annotations

Compare methods using PR-curves

adata_vis_proximity = anndata.read(f'{results_folder}LocationModelLinearDependentProximityWMultiExperimentFixedGeneAlpha_1experiments_34clusters_4039locations_10241genesalpha1/sp_with_clusters.h5ad') adata_vis_proximity.obs[adata_vis_proximity.uns['mod']['fact_names']] = adata_vis_proximity.uns['mod']['post_sample_q05']['spot_factors'] adata_vis_proximity = adata_vis_proximity[adata_vis.obs_names,:] cell2loc_proximity = adata_vis_proximity.obs[adata_vis_proximity.uns['mod']['fact_names']] #cell2loc = (cell2loc.T / cell2loc.sum(1)).T results = results + [cell2loc_proximity[col_ind]] names = names + ['cell2location proximity']

Plot consistency with Allen cell proportions (Fig 2J)

First, extract the proportion of each cell type in layers from Allen scRNA-seq data

with mpl.rc_context({'axes.facecolor': 'white', 'font.size': 10}): import seaborn as sns sns.boxplot(corrs, orient="h", palette=sc.pl.palettes.default_102).set( xlabel='Root mean squared error' ) plt.tight_layout()

Plot locations of all cell types and all methods

ct_list=list(lay_binary.index.copy())

with mpl.rc_context({'axes.facecolor': 'black', 'figure.figsize': [9, 10]}):

for m in names:
    for s in adata_vis.obs['sample'].unique():

        slide = select_slide(adata_vis, s)
        crop_x, crop_y = get_crop_coord(slide)

        if s == 'ST8059050':
            crop_x = [crop_x[0] + 50] + [crop_x[1] - 50]

        sc.pl.spatial(slide, cmap='magma',
                      color=[f'{m}_{i}' for i in ct_list], ncols=5, show=False,
                      #library_id=s,
                      size=1.3, img_key='hires', alpha_img=0, alpha=1,
                      frameon=True, legend_fontsize=50,
                      vmin=0, vmax='p99.2', save=f'all_maps_dot{s}_method{m}.pdf'
                 )

Plot locations HPC cell types in one plot with the same scale

from re import sub for c in adata_vis.uns['mod']['fact_names']: fig = plot_spatial_per_cell_type(adata_vis, cell_type=c) fig.savefig(f"{results_folder}{r['run_name']}/plots/spatial/per_cell_type/W_cell_abundance_q05_{sub('/', '_', c)}.png", bbox_inches='tight') fig.clear() plt.close(fig) f"{results_folder}{r['run_name']}/plots/spatial/per_cell_type/"

Compute fold change and p-values for Fig 2K

results[i]#[gold_standard_lay]np.mean(results[i].loc[~gold_standard_lay[:,0]].values > threshold, axis=0).mean()

Modules and their versions used for this analysis